﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Attendance.AttendanceRemedyRules;
using BCCommon;

namespace BCData.OA.Attendance.AttendanceRemedyRules
{
    public class AttendanceRemedyRulesData : IAttendanceRemedyRulesData
    {
        private IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT 
	                                    RemedyRuleId,
	                                    RuleName,
	                                    Content,
	                                    ScopeRange,
	                                    IsAllow,
	                                    LimitRemedyCount,
	                                    LimitRemedyDays,
	                                    AdminId,
	                                    AdminName,
	                                    CreateTime,
	                                    ModifierName,
	                                    ModifyTime,
	                                    Status
                                    FROM OA_AttendanceRemedyRules 
                                    WHERE Status<>'Abandon'";

        public AttendanceRemedyRulesData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        /// <summary>
        /// 获取补卡规则列表
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AttendanceRemedyRulesEntity>> GetAsync()
        {
            return await this.databaseContext.Slave.QueryAsync<AttendanceRemedyRulesEntity>(string.Format("{0}", QUERY));
        }

        /// <summary>
        /// 新创建一个补卡规则
        /// </summary>
        /// <param name="entity">补卡信息</param>
        /// <returns></returns>
        public async Task<AttendanceRemedyRulesEntity> InsertAsync(AttendanceRemedyRulesEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = SqlCommands.GetInsertSql("OA_AttendanceRemedyRules", new string[] { "RuleName", "Content", "ScopeRange", "IsAllow", "LimitRemedyCount", "LimitRemedyDays",
                "AdminId", "AdminName" });
            parameters.Add("?RuleName", entity.RuleName, DbType.AnsiString, size: 50);
            parameters.Add("?Content", entity.Content, DbType.AnsiString, size: 500);
            parameters.Add("?ScopeRange", entity.ScopeRange, DbType.AnsiString, size: 500);
            parameters.Add("?IsAllow", entity.IsAllow, DbType.Boolean);
            parameters.Add("?LimitRemedyCount", entity.LimitRemedyCount, DbType.Int32);
            parameters.Add("?LimitRemedyDays", entity.LimitRemedyDays, DbType.Int32);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64);
            parameters.Add("?AdminName", entity.AdminName, DbType.AnsiString, size: 30);

            string sqlQuery = string.Format("{0}{1} AND RemedyRuleId=LAST_INSERT_ID();", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceRemedyRulesEntity>(sqlQuery, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 修改补卡规则
        /// </summary>
        /// <param name="entity">修改信息</param>
        /// <returns></returns>
        public async Task<AttendanceRemedyRulesEntity> UpdateAsync(AttendanceRemedyRulesEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = SqlCommands.GetUpdateSql("OA_AttendanceRemedyRules", new string[] { "RuleName", "Content", "IsAllow", "LimitRemedyCount", "LimitRemedyDays", "ModifierName",
                "ModifyTime" }, new string[] { "RemedyRuleId" });
            parameters.Add("?RuleName", entity.RuleName, DbType.AnsiString, size: 50);
            parameters.Add("?Content", entity.Content, DbType.AnsiString, size: 500);
            parameters.Add("?IsAllow", entity.IsAllow, DbType.Boolean);
            parameters.Add("?LimitRemedyCount", entity.LimitRemedyCount, DbType.Int32);
            parameters.Add("?LimitRemedyDays", entity.LimitRemedyDays, DbType.Int32);
            parameters.Add("?ModifierName", entity.ModifierName, DbType.AnsiString, size: 30);
            parameters.Add("?ModifyTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?RemedyRuleId", entity.RemedyRuleId, DbType.Int64);
            string sqlQuery = string.Format("{0}{1} AND RemedyRuleId=?RemedyRuleId;", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceRemedyRulesEntity>(sqlQuery, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 获取默认第一个补卡规则
        /// </summary>
        /// <returns></returns>
        public async Task<AttendanceRemedyRulesEntity> GetFirstDefaultAsync()
        {
            string sqlQuery = string.Format("{0} LIMIT 0,1;", QUERY);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceRemedyRulesEntity>(sqlQuery);
        }

        /// <summary>
        /// 获取补卡规则详情
        /// </summary>
        /// <param name="remedyRuleId">补卡规则Id</param>
        /// <returns></returns>

        public async Task<AttendanceRemedyRulesEntity> GetByIdAsync(long remedyRuleId)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sqlQuery = string.Format("{0} AND RemedyRuleId=?RemedyRuleId;", QUERY);
            parameters.Add("?RemedyRuleId", remedyRuleId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceRemedyRulesEntity>(sqlQuery, parameters);
        }
    }
}
